package logmatcher.aspects;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.Signature;

public aspect LoggingAspect {
	/** Class logger. */
    private static final Log LOGGER = LogFactory.getLog(LoggingAspect.class);

    pointcut traceMethods()                       

        : (execution(* *.*(..))                   

          || execution(*.new(..))) && !within(LoggingAspect);       

    

    before() : traceMethods() {

        Signature sig = thisJoinPoint.getSignature();     
        
        StringBuilder builder = new StringBuilder("Entering [");
        builder.append(sig.getDeclaringType().getName());
        builder.append(".");
        builder.append(sig.getName());
        builder.append("(");

        Object[] args = thisJoinPoint.getArgs();
        
        for (int i=0;i<args.length;i++) {        	
			builder.append(args[i]);
			if(i != args.length - 1)
			builder.append(",");
		}
        builder.append(")]");
        LOGGER.debug(builder.toString());
    }
    
    after() : traceMethods() {

        Signature sig = thisJoinPoint.getSignature();     
        
        StringBuilder builder = new StringBuilder("Leaving [");
        builder.append(sig.getDeclaringType().getName());
        builder.append(".");
        builder.append(sig.getName());
        builder.append("]");
        
        LOGGER.debug(builder.toString());
    }

}